import os from pathlib import Path from PIL import Image # 1. Set your folders input_dir = Path("/Users/zhangxiaojie/PycharmProjects/IGFairness_New/_static_old") output_dir = Path("/Users/zhangxiaojie/PycharmProjects/IGFairness_New/_static") output_dir.mkdir(parents=True, exist_ok=True) MAX_SIZE = 256 # max width/height in pixels for f in input_dir.iterdir(): if not f.is_file(): continue if f.suffix.lower() not in [".png", ".bmp", ".jpg", ".jpeg"]: continue img = Image.open(f) # Resize while keeping aspect ratio img.thumbnail((MAX_SIZE, MAX_SIZE)) # Keep transparency if it exists; otherwise use RGB if img.mode not in ("RGB", "RGBA"): # Convert palette/greyscale etc. if "transparency" in img.info: img = img.convert("RGBA") else: img = img.convert("RGB") out_name = f.stem + ".png" out_path = output_dir / out_name # PNG compression (lossless but smaller file than original BMP/large PNG) img.save(out_path, format="PNG", optimize=True, compress_level=6) # 0 = least, 9 = most compression print("Saved:", out_path, "size:", os.path.getsize(out_path))